package com.tanhua.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.model.domain.Log;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;


public interface LogMapper extends BaseMapper<Log> {

    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log WHERE TYPE=#{type} AND log_time=#{logTime}")
    Integer queryByTypeAndLogTime(@Param("type") String type, @Param("logTime") String logTime); //根据操作时间和类型


    @Select("SELECT COUNT(DISTINCT user_id) FROM tb_log WHERE log_time=#{logTime}")
    Integer queryByLogTime(String logTime); //展示记录时间查询


    @Select("SELECT COUNT(DISTINCT user_id)  FROM tb_log WHERE log_time=#{today} AND user_id IN (\n " +
            " SELECT user_id FROM tb_log WHERE TYPE=\"0102\" AND log_time=#{yestoday} \n " +
            ")")
    Integer queryNumRetention1d(@Param("today") String today,@Param("yestoday") String yestoday); //查询次日留存

    //累计用户
    @Select("select count(DISTINCT (user_id)) from tb_log")
    Integer cumulativeUsers();

    //过去7天活跃用户
    @Select("SELECT COUNT( DISTINCT user_id ) FROM tb_log WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(log_time)")
    Integer activePassWeek();

    //过去30天活跃用户
    @Select("SELECT COUNT( DISTINCT user_id ) FROM tb_log WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(log_time)")
    Integer activePassMonth();

    //今日新增用户
    @Select("SELECT COUNT(type) from tb_log WHERE DATE(log_time) = CURDATE() and type = '0102'")
    Integer newUsersToday();

    //昨日新增用户
    @Select("SELECT COUNT(type) from tb_log WHERE DATE(log_time) = CURDATE()-1 and type = '0102'")
    Integer newUsersYestoday();

    //今日登录次数
    @Select("SELECT COUNT(type) from tb_log WHERE DATE(log_time) = CURDATE() and type = '0101'")
    Integer loginTimesToday();

    //昨日登录次数
    @Select("SELECT COUNT(type) from tb_log WHERE DATE(log_time) = CURDATE()-1 and type = '0101'")
    Integer loginTimesYestoday();

    //今日活跃用户
    @Select("select count(DISTINCT user_id) FROM tb_log WHERE log_time = CURDATE()")
    Integer activeUsersToday();

    //昨日活跃用户
    @Select("SELECT COUNT(DISTINCT user_id) from tb_log WHERE DATE(log_time) = CURDATE()-1")
    Integer activeUsersYestoday();
}
